package jp.bluecab.bluecabsystem.gps;

import java.util.List;

import jp.bluecab.bluecabsystem.classes.GPSRecord;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.Projection;

public class PositionOverlay extends Overlay {
	
	private double mCurrentLatitude;
	private double mCurrentLongitude;
	private double mCurrentAltitude;
	
	private boolean displayActionHistory = false;
	private List<GPSRecord> locationRecordList;
	
	private Bitmap bmp;// 描画するイメージ
	private float orientation = 0.0f;// 回転角
	
	// private GeomagneticField geomagnetic;
	
	/**
	 * コンストラクタ
	 * 
	 * @param bmp
	 *            描画するイメージ
	 */
	public PositionOverlay(Bitmap bmp) {
		this.bmp = bmp;
	}
	
	/**
	 * 緯度経度を設定する
	 */
	public void setCurrentLocation(double latitude, double longitude, double altitude) {
		mCurrentLatitude = latitude;
		mCurrentLongitude = longitude;
		mCurrentAltitude = altitude;
	}
	
	/**
	 * 回転を設定する
	 */
	public void setOrientation(float orientation) {
		this.orientation = orientation;
	}
	
	/**
	 * 行動履歴を表示するか設定する
	 * 
	 * @param displayActionHistory
	 */
	public void setDisplayActionHistory(boolean displayActionHistory) { // (1)
		// from
		this.displayActionHistory = displayActionHistory;
	}
	
	/**
	 * 行動履歴を表示しているか
	 * 
	 * @return
	 */
	public boolean isDisplayActionHistory() {
		return displayActionHistory;
	}
	
	/*
	 * 行動履歴を設定する
	 */
	public void setActionHistory(List<GPSRecord> list) {
		locationRecordList = list;
	} // (1) to
	
	@Override
	public void draw(Canvas canvas, MapView mapView, boolean shadow) {
		// SdLog.put("start");
		// 現在地を描画
		drawCurrentLocation(canvas, mapView, shadow);
		
		// 履歴を表示
		if (displayActionHistory) { // (1) from
			drawActionHistory(canvas, mapView, shadow);
		} // (1) to
		
		super.draw(canvas, mapView, shadow);
	}
	
	// private final int mRadius = 5;
	
	/**
	 * 現在地を描画する
	 */
	private void drawCurrentLocation(Canvas canvas, MapView mapView, boolean shadow) {
		// shadow = true,falseで2回呼び出される
		if (shadow) return;
		
		// SdLog.put("start");
		
		Projection projection = mapView.getProjection();
		
		Double lat = mCurrentLatitude * 1E6;
		Double lon = mCurrentLongitude * 1E6;
		
		// GeoPointを作成
		GeoPoint geoPoint = new GeoPoint(lat.intValue(), lon.intValue());
		
		// 画面の解像度にあわせて描画位置を変換する
		Point point = new Point();
		projection.toPixels(geoPoint, point);
		
		// 描画
		int centerX = point.x - bmp.getWidth() / 2;
		int centerY = point.y - bmp.getHeight() / 2;
		
		// 磁気偏角を取得する
		// 渡し元で補正済み
		// geomagnetic = new GeomagneticField(new Float(mCurrentLatitude), new
		// Float(mCurrentLongitude), new Float(mCurrentAltitude),
		// System.currentTimeMillis());
		// orientation = orientation + geomagnetic.getDeclination();
		
		// remapCoordinateSystem(inR, AXIS_X, AXIS_Y, outR);
		
		// 回転
		canvas.rotate(orientation, point.x, point.y);
		// 画像の描画
		canvas.drawBitmap(bmp, centerX, centerY, null);
		
		/*
		 * // 描画範囲設定 RectF oval = new RectF(point.x - mRadius, point.y -
		 * mRadius, point.x + mRadius, point.y + mRadius);
		 * 
		 * // 背景を設定 RectF backRect = new RectF(point.x + 2 + mRadius, point.y -
		 * 3 mRadius, point.x + 65, point.y + mRadius);
		 * 
		 * // 描画設定 Paint paint = new Paint(); paint.setARGB(255, 180, 180, 180);
		 * paint.setAntiAlias(true); paint.setFakeBoldText(true);
		 * 
		 * Paint backPaint = new Paint(); backPaint.setARGB(230, 50, 50, 50);
		 * backPaint.setAntiAlias(true);
		 * 
		 * // 現在地を描画 canvas.drawOval(oval, paint);
		 * canvas.drawRoundRect(backRect, 5, 5, backPaint);
		 * canvas.drawText("現在地", point.x + 2 * mRadius, point.y, paint);
		 */
	}
	
	@Override
	public boolean onTap(GeoPoint point, MapView mapView) {
		return false;
	}
	
	/**
	 * 行動履歴を描画する
	 * 
	 * @param canvas
	 * @param mapView
	 * @param shadow
	 */
	private void drawActionHistory(Canvas canvas, MapView mapView, boolean shadow) {
		
		Projection projection = mapView.getProjection();
		
		// 描画するペンを設定
		Paint paint = new Paint();
		paint.setARGB(128, 0, 255, 0);
		paint.setStrokeWidth(3.0f);
		paint.setAntiAlias(true);
		
		int cnt = 0;
		float oldpx = 0, oldpy = 0;
		for (GPSRecord loc : locationRecordList) {
			
			double lat = loc.getLatitude() * 1E6;
			double lon = loc.getLongitude() * 1E6;
			
			GeoPoint geoPoint = new GeoPoint((int) lat, (int) lon);
			
			// 画面の解像度にあわせて位置を変換する
			Point point = new Point();
			projection.toPixels(geoPoint, point);
			
			if (cnt > 0) {
				// 線を描画する
				canvas.drawLine(oldpx, oldpy, point.x, point.y, paint);
			}
			
			cnt++;
			oldpx = point.x;
			oldpy = point.y;
		}
	} // (2) to
}
